using System.Data.Common;
using Dapper.Extensions;
using Microsoft.Extensions.Logging;
using static System.Environment;

namespace Dapper.Extensions.Configuration
{
    public class DbLoggingConfiguration(
	    LogLevel logLevel,
	    string openConnectionMessage,
	    string closeConnectionMessage,
	    string executeQueryMessage,
	    bool logSensitiveData = false,
	    Func<DbConnection, object>? connectionProjector = null)
    {
        public static readonly DbLoggingConfiguration Default = new(
            logLevel: LogLevel.Information,
            openConnectionMessage: "Dapper connection: open, elapsed: {elapsed} ms, " +
                                   "context: {@context}, connection: {@connection}",
            closeConnectionMessage: "Dapper connection: close, elapsed: {elapsed} ms, " +
                                    "context: {@context}, connection: {@connection}",
            executeQueryMessage: $"Dapper query:{NewLine}{{query}}{NewLine}" +
                                 "Parameters: {params}, elapsed: {elapsed} ms, " +
                                 "context: {@context}, connection: {@connection}",
            logSensitiveData: false,
            connectionProjector: _ => Empty.Object);

        public LogLevel LogLevel { get; } = logLevel;
        public string OpenConnectionMessage { get; } = openConnectionMessage;
        public string CloseConnectionMessage { get; } = closeConnectionMessage;
        public string ExecuteQueryMessage { get; } = executeQueryMessage;
        public bool LogSensitiveData { get; } = logSensitiveData;
        public Func<DbConnection, object>? ConnectionProjector { get; } = connectionProjector;
    }
}
